/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jeff.springWebTestNG.ioc.autowired;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

/**
 *
 * @author winnie
 */
@Component
public class FactoryMethodComponent {

  private static int i;

  @Bean @Qualifier("public")
  public FlyBean publicInstance() {
      return new FlyBean("publicInstance");
  }

  // use of a custom qualifier and autowiring of method parameters

  @Bean
  protected FlyBean protectedInstance(@Qualifier("public") FlyBean spouse,
                                       @Value("#{privateInstance.age}") String country) {
      FlyBean tb = new FlyBean("protectedInstance", 1);
      tb.setSpouse(tb);
      tb.setCountry(country);
      return tb;
  }

  @Bean @Scope(BeanDefinition.SCOPE_SINGLETON)
  private FlyBean privateInstance() {
      return new FlyBean("privateInstance", i++);
  }

  @Bean @Scope(value = WebApplicationContext.SCOPE_SESSION,
               proxyMode = ScopedProxyMode.TARGET_CLASS)
  public FlyBean requestScopedInstance() {
      return new FlyBean("requestScopedInstance", 3);
  }
}
